# Created by Cary Landers
# March 29 2010

# This script is designed to take changes exported from a SharePoint list through MS Access
# and then import those changes to Active Directory. All changes are recorded in the
# report.txt file.

$ErrorActionPreference = "SilentlyContinue"

# Path for the csv import
$aryUser= import-csv -Path qry_Export.csv
$Class = "User"
# Domain Name
$dc = ",dc=domain,dc=local"
$date = date
foreach($strUser in $aryUser)
{
$ou = $strUser.LDAP
$cnuser="cn="+$($strUser.User)+","
$User = [ADSI]"LDAP://$cnuser$ou$dc" 

write-host -foregroundcolor blue ============================================================
write-host -foregroundcolor green Starting update for $cnuser

# Adds a note to the users Info section.
$User.Put("Info", "Modified by the Sharepoint Company Directory on $date")

# Error check to see if the user exist by the Office Location. 
# Not sure if this is the best method but it works.	
if ($strUser.physicalDeliveryOfficeName -ne '')
{$User.Put("physicalDeliveryOfficeName", $($strUser.physicalDeliveryOfficeName))
TRAP [SystemException]{
	Write-output "=============================================" >> report.txt
	Write-output "Error found for $cnuser - $date" >> report.txt
	Write-output "LDAP://$cnuser$ou$dc" >> report.txt
	}
}

# Set the users street address
if ($strUser.streetAddress -ne '') 
{
$User.Put("streetAddress", $($strUser.streetAddress))
}
# Set the users login
if ($strUser.L -ne '')
{$User.Put("L", $($strUser.L))
}
# Set the users title
if ($strUser.title -ne '')
{$User.Put("title", $($strUser.title)) 
}
# Set the department.
if ($strUser.department -ne '')
{#$User.Put("department", $($strUser.department))
}
# Set the users postal code
if ($strUser.postalcode -ne '')
{$User.Put("postalcode", $($strUser.postalcode))
}
# Set the users company
if ($strUser.Company -ne '')
{$User.Put("company", $($strUser.Company))
}
# Set the users work number
if ($strUser.telephoneNumber -ne '')
{$User.Put("telephoneNumber", $($strUser.telephoneNumber))
}
# Set the users home phone number
if ($strUser.homePhone -ne '')
{$User.Put("homePhone", $($strUser.homePhone))
}
# Set the users mobile number
if ($strUser.mobile -ne '')
{$User.Put("Mobile", $($strUser.mobile))
}
# Set the users fax number
if ($strUser.facsimileTelephoneNumber -ne '')
{$User.Put("facsimileTelephoneNumber", $($strUser.facsimileTelephoneNumber))
}
# Set the users state.
if ($strUser.St -ne '')
{$User.Put("St", $($strUser.St))
}
$User.Put("C", "US")
$User.Put("co", "United States")
$User.setInfo()

write-host -foregroundcolor green $cnuser$ou$dc
write-host -foregroundcolor green is DONE!

}